<?php
namespace app\index\controller;
use think\Controller;
use think\request;
use think\Db;
use app\service\lifeapiService;//生活号接口
use app\service\shopService;
use app\service\appletService;
use app\shop\controller\Common;
use printing\printShop;
use think\Log;
class Bill extends Common{
	public function index(){
		session("url",request()->url());
		$communityid=request()->get("community_id");
		$communityinfo=Db::table("community")->where("community_id",$communityid)->find();
		if(empty($communityinfo)){
			return json(["error"=>"小区信息错误"]);
		}
        $group=Db::table('roominfo')->distinct(true)->field('zz_group')->where("community_id",$communityid)->select();

        $this->assign("communityinfo",$communityinfo);
        $this->assign("group",$group);
        return View::fetch();
	}
	//账单详情
	public function details(){
		session("url",request()->url());
		$room_id=request()->param("roomid");
		$roominfo=Db::table("roominfo")->where("id",$room_id)->find();
		
		$billlis = (new appletService())->billRoomIdList($roominfo['out_room_id']);
		//$billlis= Db::name('bill')->where('out_room_id',$userinfo['out_room_id'])->where('status','WAIT_PAYMENT')->where('id','>','261216')->select();
		if(empty($billlis)){
			return View::fetch("nopaymsg");
		}
		$billlist = (new appletService())->cost_type($roominfo['out_room_id']);
		
		$communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
		
		$this->assign("communityinfo",$communityinfo);
		$this->assign("billlist",$billlis);
		$this->assign("roominfo",$roominfo);
		return View::fetch();
	}
	//计算要缴费的金额
	public function moneysum(){
		
		if(!(request()->has('billid'))){
			return json(['state'=>'0','info' => '请选择要交费的账单']);
		}
		$post=request()->post();
		
		$ordermoney=0;
		foreach ($post["billid"] as $key => $value) {
			$billinfo=Db::table("bill")->where('id',$value)->find();
			$ordermoney=$ordermoney+$billinfo["bill_entry_amount"];
		}
		
		if($ordermoney<=0){
			return json(['state'=>'0','info' => '缴费金额必须大于0']);
		}
		$data["pay_money"]=round($ordermoney,2);
		return json(['state'=>'1','info' => $data["pay_money"]]);
	}

	public function createOrder(){

		
		if(!(request()->has('billid'))){
			return json(['info' => '请选择要交费的账单']);
		}
		$post=request()->post();
		$data["bill_id"]=serialize($post["billid"]);
		
		$community_name="";
        $community_id="";
		$room_address ="";
        $out_room_id ="";
		$ordermoney=0;
		foreach ($post["billid"] as $key => $value) {
			$billinfo=Db::table("bill")->where('id',$value)->find();
			$ordermoney=$ordermoney+$billinfo["bill_entry_amount"];

			$community_name = (new billService())->communityMachine($value['community_id'])["community_name"];
			$community_id=$value["community_id"];
			$room_address =$value["room_address"];
        	$out_room_id =$value["out_room_id"];
		}
		$data["order_money"]=$ordermoney;
		$data["buyer_id"]=$userinfo["user_id"];
		
		$data["out_room_id"]=$userinfo["out_room_id"];
		$data["room_address"]=$userinfo["room_address"];
		$data["community_id"]=$userinfo["community_id"];
		$data["community_name"]=$userinfo["community_name"];
		$data["point_money"]=round($data["point"]/100,2);//优惠金额
		if($data["point_money"]>=$data["order_money"]){
			return json(['info' => '缴费金额必须大于0']);
		}
		$data["pay_money"]=round($data["order_money"]-$data["point_money"],2);
		$data["out_order_no"]=date('YmdHiss').rand(100000,999999);

		$api = new lifeapiService();
		$token=(new shopService())->communityToken($data["community_id"]);
		$notifyurl='https://www.jfzhiwen.com/index/ali/billnotice';
		$result = $api->orderCreate(json_encode(['out_trade_no' =>$data['out_order_no'], 'total_amount' =>  $data['pay_money'], 'buyer_id' =>  $data['buyer_id'],'subject' => $data["community_name"].$data["room_address"].'缴费', 'token' => $token,'notifyurl' => $notifyurl]));
		if(property_exists($result,'sub_msg')){	
			return json(['info' => $result->sub_msg]);
		}else{
			$data['trade_no'] =  $result->trade_no;
			$data["logon_id"]=$userinfo["logon_id"];
			$data["user_name"]=$userinfo["user_name"];	
			$res=Db::table("zz_bill_order")->insert($data);

			$billpay["user_id"]=session("user_id");
            $billpay["order_money"]=$ordermoney;
            $billpay["pay_money"]=$ordermoney;
            $billpay["out_trade_no"]=$data['out_trade_no'];
            $billpay["billid"]=$data["bill_id"];
            $billpay["out_room_id"]=$out_room_id["0"];
            $billpay["room_address"]=$room_address["0"];
            $billpay["community_name"]=$community_name;
            $billpay["community_id"]=$community_id;
            
            $res=Db::table("billpay")->insert($billpay);
			//返回结果到前台唤起收银台
			if($res){
				return json(['order_no' => $data['trade_no']]);
			}else{
				return json(['info' => '订单存储失败']);
			}
		}
		
	}
	
			

}
